class <%= class_name %>Controller < ApplicationController
  getter <%= @name %> = <%= class_name %>.new

  before_action do
    only [:show, :edit, :update, :destroy] { set_<%= @name %> }
  end

  def index
    <%= name_plural %> = <%= class_name %>.all
    render "index.<%= config.language %>"
  end

  def show
    render "show.<%= config.language %>"
  end

  def new
    render "new.<%= config.language %>"
  end

  def edit
    render "edit.<%= config.language %>"
  end

  def create
    <%= @name %> = <%= class_name %>.new <%= @name %>_params.validate!
    if <%= @name %>.save
      redirect_to action: :index, flash: {"success" => "<%= @name.capitalize %> has been created."}
    else
      flash[:danger] = "Could not create <%= class_name %>!"
      render "new.<%= config.language %>"
    end
  end

  def update
    <%= @name %>.set_attributes <%= @name %>_params.validate!
    if <%= @name %>.save
      redirect_to action: :index, flash: {"success" => "<%= @name.capitalize %> has been updated."}
    else
      flash[:danger] = "Could not update <%= class_name %>!"
      render "edit.<%= config.language %>"
    end
  end

  def destroy
    <%= @name %>.destroy
    redirect_to action: :index, flash: {"success" => "<%= @name.capitalize %> has been deleted."}
  end

  private def <%= @name %>_params
    params.validation do
      <%- @fields_hash.keys.each do |k| -%>
      required :<%= k %>
      <%- end -%>
    end
  end

  private def set_<%= @name %>
    @<%= @name %> = <%= class_name %>.find! params[:id]
  end
end
